package com.young.common.query;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/28/24 4:30 PM
 */
public class Query implements Serializable {
    private static final int DEFAULT_PAGE_SIZE = 20;
    protected int page = -1;
    protected int pageSize = 20;
    protected Map<String, Object> parameters = new HashMap();

    protected Query() {
    }

    public int getPage() {
        return this.page;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public Map<String, Object> getParameters() {
        return this.parameters;
    }

    public Object get(String key) {
        return this.parameters.get(key);
    }

    public void put(String name, Object value) {
        if (name != null) {
            this.parameters.put(name, value);
        }

    }

    public Map<String, Object> buildParameters() {
        Map<String, Object> _parameters = new HashMap(this.getParameters());
        if (this.pageable()) {
            int startRow = (this.page - 1) * this.pageSize;
            _parameters.put("startRow", startRow);
            _parameters.put("pageSize", this.pageSize);
        }

        return _parameters;
    }

    protected boolean pageable() {
        return this.page > 0 && this.pageSize > 0;
    }

    protected Map<String, Object> render() {
        Map<String, Object> _parameters = new HashMap(this.getParameters());
        if (this.pageable()) {
            _parameters.put("page", this.getPage());
            _parameters.put("pageSize", this.getPageSize());
        }

        return _parameters;
    }

    public Query next() {
        Query next = new Query();
        if (this.pageable()) {
            next.page = this.page + 1;
            next.pageSize = this.pageSize;
            next.parameters = new HashMap(this.parameters);
        }

        return next;
    }
}
